/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "CommandUtil.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

/**
**/
CommandUtil::CommandUtil()
{
	QCoreApplication::postEvent( this, new QEvent( QEvent::User ) );
}

/**
**/
CommandUtil::~CommandUtil()
{
}

/**
**/
bool CommandUtil::event( QEvent *e )
{
	if( e->type() == QEvent::User ){
		if( exec() )
			qApp->exit( 0 );
		else
			qApp->exit( 1 );		
		return true;
	}

	return QObject::event( e );
}

/**
**/
QStringListIterator CommandUtil::getGeneralArgs() const
{
	return QStringListIterator( m_generalArgs );
}

/**
**/
void CommandUtil::registerArg( const QString &key, QString &val, bool required )
{
	Argument	&arg = m_args[key];

	arg.m_arg = &val;
	arg.m_required = required;
}

/**
**/
void CommandUtil::registerFlag( const QString &key, bool &val )
{
	m_flags[key] = &val;
}

/**
**/
void CommandUtil::error( const QString &msg )
{
	output( QString( "Error: " ) + msg );
}

/**
**/
void CommandUtil::warning( const QString &msg )
{
	output( QString( "Warning: " ) + msg );
}

/**
**/
void CommandUtil::output( const QString &msg )
{
	QByteArray	bytes = msg.toAscii();

	printf( "%s\n", bytes.data() );
}

/**
**/
bool CommandUtil::parseArgs()
{
	QStringListIterator	argIt( qApp->arguments() );

	argIt.next(); // skip the first argument

	while( argIt.hasNext() ){
		QString	arg = argIt.next();

		if( arg[0] == '-' ){
			arg = arg.mid( 1 );
			if( m_args.contains( arg ) ){
				if( argIt.hasNext() )
					*(m_args[arg].m_arg) = argIt.next();
				else {
					error( QString( "No value specified for '%1' parameter." ).arg( arg ) );
					return false;
				}
			}
			else if( m_flags.contains( arg ) ){
				*(m_flags[arg]) = true;
			}
			else {
				error( QString( "Unrecognized parameter '%1'" ).arg( arg ) );
				return false;
			}
		}
		else {
			m_generalArgs.append( arg );
		}
	}

	QHashIterator<QString,Argument>	keyIt( m_args );

	while( keyIt.hasNext() ){
		keyIt.next();
		const Argument	&arg = keyIt.value();

		if( arg.m_required && arg.m_arg->isEmpty() ){
			error( QString( "No value specified for parameter: '%1'" ).arg( keyIt.key() ) );
			return false;
		}
	}

	return true;
}

/**
**/
bool CommandUtil::exec()
{
	if( !parseArgs() ){
		printUsage();
		return false;
	}

	return run();
}
